package cn.hyy.FVOnline.controller;

import cn.hyy.FVOnline.entity.Goods;
import cn.hyy.FVOnline.service.GoodsService;
import cn.hyy.FVOnline.entity.PageEntitys;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;


@Controller
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @RequestMapping(value = "/getPageGoods", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String getPageGoods(@RequestParam("shopid") String shopid, @RequestParam("page")String page , @RequestParam("pageSize")String pageSize){
        PageEntitys pageEntity = goodsService.findPageGoods(Integer.parseInt(shopid), Integer.parseInt(page), Integer.parseInt(pageSize));
        return JSON.toJSONString(pageEntity);
    }

    @RequestMapping(value = "/getByTypeName", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String getByTypeName(@RequestParam("shopid") String shopid, @RequestParam("page")String page , @RequestParam("pageSize")String pageSize, @RequestParam("type")String type, @RequestParam("goodsname")String goodsname){
        PageEntitys pageEntity = goodsService.findByTypeName(Integer.parseInt(shopid), Integer.parseInt(page), Integer.parseInt(pageSize),type,goodsname);
        return JSON.toJSONString(pageEntity);
    }

    @RequestMapping(value = "/deleteGoods", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String deleteGoods(@RequestParam("id") String id){

        JSONObject jo = new JSONObject();

        try {
            goodsService.deleteGoods(Integer.parseInt(id));

            jo.put("code","success");
            jo.put("message","删除成功");

        }catch (Exception e){
            jo.put("code","error");
            jo.put("message","删除失败");
        }

        return jo.toJSONString();
    }

    @RequestMapping(value = "/updateGoods", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String updateGoods(@RequestParam("id") String id, @RequestParam("name") String name, @RequestParam("details") String details, @RequestParam("price") String price, @RequestParam("number") String number, @RequestParam("type") String type){
        JSONObject jo = new JSONObject();

        try {

            Goods goods = new Goods();
            goods.setId(Integer.parseInt(id));
            goods.setGoodsname(name);
            goods.setDetails(details);
            goods.setPrice(Double.parseDouble(price));
            goods.setNumber(Integer.parseInt(number));
            goods.setType(type);

            goodsService.updateGoods(goods);

            jo.put("code","success");
            jo.put("message","修改成功");

        }catch (Exception e){
            jo.put("code","error");
            jo.put("message","修改失败");
        }

        return jo.toJSONString();
    }


    @RequestMapping(value = "/newGoods", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String newGoods(@RequestParam("goodsname") String goodsname,@RequestParam("details") String details,@RequestParam("shopid") String shopid,@RequestParam("price") String price,@RequestParam("number") String number,@RequestParam("type") String type,@RequestParam(value = "file",required=false) MultipartFile file){

        JSONObject jo = new JSONObject();

        try {
            Goods goods = new Goods();
            goods.setGoodsname(goodsname);
            goods.setDetails(details);
            goods.setShopid(Integer.parseInt(shopid));
            goods.setPrice(Double.parseDouble(price));
            goods.setNumber(Integer.parseInt(number));
            goods.setType(type);
            goodsService.newGoods(goods,file);
            jo.put("code","success");
            jo.put("message","提交审核成功");
        }catch (Exception e){
            jo.put("code","error");
            jo.put("message","提交审核失败");
        }
        return jo.toJSONString();
    }


    @RequestMapping("/getImg")
    public @ResponseBody void getHS(@RequestParam("goodsId") String goodsId, HttpServletResponse response){

        try {

            BufferedImage bi = goodsService.findImg(goodsId);

            ImageIO.write(bi,"PNG",response.getOutputStream());
        }catch (Exception e){
        }

    }

    @RequestMapping(value = "/getByName", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String getByName(@RequestParam("shopid") String shopid, @RequestParam("page")String page , @RequestParam("pageSize")String pageSize, @RequestParam("goodsname")String goodsname){
        PageEntitys pageEntity = goodsService.findByName(Integer.parseInt(shopid), Integer.parseInt(page), Integer.parseInt(pageSize),goodsname);
        return JSON.toJSONString(pageEntity);
    }

    @RequestMapping(value = "/getGoodsList", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String getGoodsList(@RequestParam("page")String page , @RequestParam("pageSize")String pageSize, @RequestParam("what")String goodsname){
        PageEntitys pageEntity = goodsService.findGoodsList(Integer.parseInt(page), Integer.parseInt(pageSize),goodsname);
        return JSON.toJSONString(pageEntity);
    }

    @RequestMapping(value = "/getById", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String getById(@RequestParam("goodsid") String goodsid){

        Goods goods = goodsService.findById(Integer.parseInt(goodsid));

        return JSON.toJSONString(goods);

    }


    @RequestMapping(value = "/delGoods", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String delGoods(@RequestParam("id") String id){

        JSONObject jo = new JSONObject();

        try {
            goodsService.delGoods(Integer.parseInt(id));

            jo.put("code","success");
            jo.put("message","商品封禁成功");

        }catch (Exception e){
            jo.put("code","error");
            jo.put("message","商品封禁失败");
        }

        return jo.toJSONString();
    }


    @RequestMapping(value = "/getGoodsList2", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String getGoodsList2(@RequestParam("page")String page , @RequestParam("pageSize")String pageSize, @RequestParam("what")String goodsname){
        PageEntitys pageEntity = goodsService.findGoodsList2(Integer.parseInt(page), Integer.parseInt(pageSize),goodsname);
        return JSON.toJSONString(pageEntity);
    }


    @RequestMapping(value = "/addGoods", produces = "text/html;charset=UTF-8;")
    public @ResponseBody String addGoods(@RequestParam("id") String id){

        JSONObject jo = new JSONObject();
        try {
            goodsService.addGoods(Integer.parseInt(id));
            jo.put("code","success");
            jo.put("message","商品审核成功");
        }catch (Exception e){
            jo.put("code","error");
            jo.put("message","商品审核失败");
        }
        return jo.toJSONString();
    }

}
